package com.example.tellwin.mine.act;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.tellwin.AppHelper;
import com.example.tellwin.IdentitySelectActivity;
import com.example.tellwin.MainActivity;
import com.example.tellwin.MainApplication;
import com.example.tellwin.R;
import com.example.tellwin.base.CpBaseActivty;
import com.example.tellwin.base.DaggerRxComponent;
import com.example.tellwin.common.Common;
import com.example.tellwin.mine.bean.UserBean;
import com.example.tellwin.mine.contract.LoginRegisterContract;
import com.example.tellwin.mine.presenter.LoginRegisterPresenter;
import com.example.tellwin.utils.ToastUtil;

import java.net.IDN;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginRegisterActivity extends CpBaseActivty implements LoginRegisterContract.View {

    @BindView(R.id.logo_iv)
    ImageView logoIv;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.verification_code_et)
    EditText verificationCodeEt;
    @BindView(R.id.get_verification_code_tv)
    TextView getVerificationCodeTv;
    @BindView(R.id.commit_btn)
    TextView commitBtn;
    @BindView(R.id.user_agreement_tv)
    TextView userAgreementTv;

    boolean isLogin = true;
    @BindView(R.id.jump_tv)
    TextView jumpTv;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.invitation_code_et)
    EditText invitationCodeEt;

    @Inject
    LoginRegisterPresenter mPresenter;

    CountDownTimer countDownTimer;
    boolean isStartTimer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_register);
        ButterKnife.bind(this);
        initViews();
        initEvents();
        initDatas();
    }

    @Override
    protected void initViews() {
        DaggerRxComponent.builder()
                .baseAppComponent(MainApplication.getAppComponent())
                .build()
                .inject(this);
        mPresenter.attachView(this);
    }

    @Override
    protected void initEvents() {
        commitBtn.setEnabled(false);
        getVerificationCodeTv.setEnabled(false);
        phoneEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() == 11) {
                    getVerificationCodeTv.setSelected(true);
                    getVerificationCodeTv.setEnabled(true);
                } else {
                    getVerificationCodeTv.setSelected(false);
                    getVerificationCodeTv.setEnabled(false);
                }
                setRegisterBtnEnable();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        verificationCodeEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                setRegisterBtnEnable();
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        jumpTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLogin) {
                    Intent intent = new Intent(LoginRegisterActivity.this, LoginRegisterActivity.class);
                    intent.putExtra(Common.IS_LOGIN, false);
                    startActivity(intent);
                } else {
                    finish();
                }
            }
        });
    }

    @Override
    protected void initDatas() {

        isLogin = getIntent().getBooleanExtra(Common.IS_LOGIN, true);
        if (isLogin) {
            setTitle(R.string.login);
            commitBtn.setText(R.string.login);
            jumpTv.setText(R.string.register_account);
            backIv.setVisibility(View.GONE);
            invitationCodeEt.setVisibility(View.GONE);
        } else {
            setTitle(R.string.register_account);
            commitBtn.setText(R.string.register);
            jumpTv.setText(R.string.login);
            invitationCodeEt.setVisibility(View.VISIBLE);
        }
    }

    private void setRegisterBtnEnable() {
        if (phoneEt.getText().toString().length() == 11 && verificationCodeEt.getText().toString().length() == 6) {
            commitBtn.setEnabled(true);
        } else {
            commitBtn.setEnabled(false);
        }
    }

    @OnClick({R.id.get_verification_code_tv, R.id.commit_btn, R.id.user_agreement_tv, R.id.jump_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.get_verification_code_tv:
                if (isStartTimer) {
                    return;
                }
                if (phoneEt.getText().toString().length() != 11) {
                    ToastUtil.show(this, R.string.input_phone);
                    return;
                }
                int type = isLogin ? 2 : 1;
                mPresenter.getVerificationCode(phoneEt.getText().toString(), type);
                if (countDownTimer == null) {
                    countDownTimer = new CountDownTimer(60 * 1000 + 50, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            int time = (int) (millisUntilFinished / 1000);
                            getVerificationCodeTv.setText(time + "秒");
                        }

                        @Override
                        public void onFinish() {
                            getVerificationCodeTv.setText(R.string.get_verification_code);
                            isStartTimer = false;
                        }
                    };
                }
                isStartTimer = true;
                countDownTimer.start();
                break;
            case R.id.commit_btn:
                if (isLogin) {
                    mPresenter.login(phoneEt.getText().toString(), verificationCodeEt.getText().toString());
                } else {
                    mPresenter.register(phoneEt.getText().toString(), verificationCodeEt.getText().toString(), invitationCodeEt.getText().toString());
                }
                break;
            case R.id.user_agreement_tv:
                Intent intent = new Intent(this, UserAgreementActivity.class);
                startActivity(intent);
                break;
            case R.id.jump_tv:

                break;
        }
    }


    @Override
    public void loginResult(UserBean userBean) {
        //  ToastUtil.show(LoginRegisterActivity.this, "短信验证码不正确");
        if (userBean.getIsIdentity() == 0) {
            Intent intent = new Intent(LoginRegisterActivity.this, IdentitySelectActivity.class);
            startActivity(intent);
            finish();
        } else {
            Intent intent = new Intent(LoginRegisterActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void verificationCodeResult() {

    }

    @Override
    public void registerResult(UserBean userBean) {
        Intent intent = new Intent(LoginRegisterActivity.this, IdentitySelectActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }
}
